package com.gry.stusys;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        boolean flag;
        ArrayList<Student> list = new ArrayList<>();
        do {
            menu();
            Scanner sc = new Scanner(System.in);
            System.out.println("输入你的选择");
            int choose = sc.nextInt();
            flag = true;
            switch (choose) {
                case 1:
                    System.out.println("输入学生id,姓名,年龄,地址");
                    list = addStudent(list);
                    printStudent(list);
                    break;
                case 2:
                    delete(list);
                    break;
                case 3:
                    System.out.println("修改学生");
                    modify(list);
                    break;
                case 4:
                    query(list);
                    break;
                case 0:
                    flag = false;
                    System.out.println("已退出");
                    break;
                default:
                    System.out.println("输入有误请重新输入");
                    break;
            }
        } while (flag);
    }


    public static void menu() {

        System.out.println("-----欢迎来到巩润阳学生管理系统----");
        System.out.println("-----1.添加学生---2.删除学生------");
        System.out.println("-----3.修改学生---4.查询学生------");
        System.out.println("-------------0.退出--------------");
    }

    public static void modify(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要修改的用户id");
        String id = sc.next();
        boolean contains = contains(list, id);
        if (contains) {
            int index = getIndex(list, id);
            System.out.println("输入修改后的姓名,年龄,家庭住址");
            list.get(index).setName(sc.next());
            list.get(index).setAge(sc.nextInt());
            list.get(index).setAddress(sc.next());
        } else {
            System.out.println("该用户不存在,请重新输入");
        }
    }

    public static void query(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息，请添加后再查询");
        } else {
            printStudent(list);
        }
    }

    //删除
    private static void delete(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的id");
        String id = sc.next();
        boolean contains = contains(list, id);
        if (contains) {
            int index = getIndex(list, id);
            list.remove(index);
        } else {
            System.out.println("要删除的id不存在");
        }
    }

    public static ArrayList<Student> addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        student.setId(sc.next());
        student.setName(sc.next());
        student.setAge(sc.nextInt());
        student.setAddress(sc.next());
        //判断表中有没有数据
        if (list.size() != 0) {
            //有数据判断
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(student.getId())) {
                    System.out.println("id已存在,请重新输入");
                    return list;
                } else {
                    list.add(student);
                }
            }
        } else {
            //没有数据直接添加
            list.add(student);
        }
        return list;
    }

    //判断是否存在:
    public static boolean contains(ArrayList<Student> list, String id) {
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getId().equals(id)) {
//                return true;
//            }
//        }
//        return false;
        return getIndex(list, id) >= 0;
    }

    //获得索引
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    //打印:
    public static void printStudent(ArrayList<Student> list) {
        System.out.println("id\t\t\t姓名\t\t年龄\t\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + ",\t" + list.get(i).getName() + ",\t" + list.get(i).getAge() + ",\t" +
                    list.get(i).getAddress());
        }
    }
}
